using System;
using System.Text;
using System.Threading;
using System.ComponentModel;
using System.Collections.Generic;
using System.Runtime.InteropServices;

namespace Demo_G.O.S.E.ServerEngine.Common
{
    /// <summary>
    /// 
    /// </summary>
    public class HiPerfTimer
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="lpPerformanceCount"></param>
        /// <returns></returns>
        [DllImport( "Kernel32.dll" )]
        private static extern bool QueryPerformanceCounter( out long lpPerformanceCount );

        /// <summary>
        /// 
        /// </summary>
        /// <param name="lpFrequency"></param>
        /// <returns></returns>
        [DllImport( "Kernel32.dll" )]
        private static extern bool QueryPerformanceFrequency( out long lpFrequency );

        /// <summary>
        /// 
        /// </summary>
        private long m_StartTime;
        /// <summary>
        /// 
        /// </summary>
        private long m_StopTime;
        /// <summary>
        /// 
        /// </summary>
        private long m_Frequency;

        /// <summary>
        /// Constructor
        /// </summary>
        public HiPerfTimer()
        {
            m_StartTime = 0;
            m_StopTime = 0;

            if ( QueryPerformanceFrequency( out m_Frequency ) == false )
            {
                // high-performance counter not supported
                throw new Win32Exception();
            }
        }

        /// <summary>
        /// Start the timer
        /// </summary>
        public void Start()
        {
            QueryPerformanceCounter( out m_StartTime );
        }

        /// <summary>
        /// Stop the timer
        /// </summary>
        public void Stop()
        {
            QueryPerformanceCounter( out m_StopTime );
        }

        /// <summary>
        /// Returns the duration of the timer (in seconds)
        /// </summary>
        public double Duration
        {
            get { return (double)( m_StopTime - m_StartTime ) / (double)m_Frequency; }
        }
    }
}
